import 'package:app/components/tree/treeItemButton.dart';
import 'package:app/util/StringUtils.dart';
import 'package:flutter/material.dart';
import './treeItem.dart';
import 'package:collection/collection.dart';
import '../button/index.dart';

class Tree extends Column{

  late List<TreeItem> items;

  Tree({
    super.key,
    String? title,
    required this.items,
    Button? button,
    BuildContext? context,
    bool? isShowCheckbox,
    Function(bool expand, int index, TreeItem? item)? onChange,
    Function(bool checked, int index, int childIndex, int level)? onCheck,
  }) : super(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        getTitle(title ?? ""),
        Expanded(
            child: ListView.builder(
              itemBuilder: (context, index) {
                return getChildren(items, index, onChange, onCheck, button, context, isShowCheckbox);
              },
              itemCount: items.length,
            )
        )
      ],
  );

  static Widget getTitle(String title) {
    if (StringUtils.isNotNull(title)) {
      return Padding(
          padding: const EdgeInsets.only(top: 10, bottom: 10, left: 10),
          child: Container(
            alignment: Alignment.centerLeft,
            child: Text(title ?? '',
                style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.w600,
                    color: Color.fromARGB(255, 128, 128, 128))),
          ));
    }
    return Container();
  }

  static Widget getChildren(List<TreeItem> items,int index, onChange, onCheck, Button? button, BuildContext context, isShowCheckbox){
    return Container(
        decoration: BoxDecoration(
            color: const Color.fromARGB(255, 255, 255, 255),
            borderRadius:  BorderRadius.circular(10.0)
        ),
        child: GestureDetector(
          onTap: ()=>{onCheck(items[index].checked??false, index, 0, 1)},
          behavior: HitTestBehavior.opaque,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Row(
                children: [
                  Expanded(
                    flex: 3,
                    child: Container(
                      height: 50,
                      decoration: const BoxDecoration(
                        border: Border(
                          bottom: BorderSide(color: Color.fromARGB(255, 243, 243, 243), width: 1.0), // 设置下边框样式
                        ),
                      ),
                      child: Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(left: 10),
                            child: Row(
                              children: [
                                GestureDetector(
                                  onTap: (){onChange(items[index].expand, index, items[index]);},
                                  child: Container(
                                    child: StringUtils.isNotNull(items[index].expand) && items[index].expand! ?
                                    const Icon(Icons.keyboard_arrow_down, size: 30,)
                                        : const Icon(Icons.keyboard_arrow_right, size: 30,),
                                  ),
                                ),
                                Row(
                                  children: [
                                    getCheckbox(items, index, onCheck, isShowCheckbox),
                                    Padding(
                                        padding: const EdgeInsets.only(left: 0),
                                        child: Text(
                                          items[index].label??'',
                                          style: TextStyle(
                                            fontSize: 15,
                                            fontWeight: FontWeight.w600,
                                            color: getColor(items[index].checked??false),
                                          ),
                                        )
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  ...getButtons(items[index].buttons??[], index, 0, 1)
                ],
              ),
              ...getChild(items[index].children??[],  items[index].expand??false, index, onCheck),
              getOperation(items, index, button, context),
            ],
          ),
        )
    );
  }

  // 获取颜色
  static Color getColor(bool checked){
    if(checked){
      return const Color.fromARGB(255, 69, 228, 194);
    }
    return Colors.grey;
  }

  /// 是否显示check
  static Widget getCheckbox(items, index, onCheck, isShowCheckbox){
    if(StringUtils.isNotNull(isShowCheckbox) && isShowCheckbox){
      return  Checkbox(
          value: items[index].checked??false,
          onChanged: (value) => onCheck(value, index, 0, 1),
          activeColor: const Color.fromARGB(255, 69, 228, 194)
      );
    }
    return Container();
  }

  /// 操作按钮
  static Widget getOperation(List<TreeItem> items, int index, Button? button, BuildContext? context){
    if(index == items.length-1 && StringUtils.isNotNull(button) && StringUtils.isNotNull(context)){
      return Container(
          width: MediaQuery.of(context!).size.width,
          child: Padding(
            padding: const EdgeInsets.only(left: 10, top: 20, bottom: 10, right: 10),
            child: button,
          ),
      );
    }
    return Container();
  }

  static List<Widget> getButtons(List<TreeItemButton> items, int index, int childIndex, int level){
    return items.map((item) => Expanded(
        flex: 2,
        child: Container(
            height: item.isShow! ? null : 50,
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Color.fromARGB(255, 243, 243, 243), width: 1.0), // 设置下边框样式
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.only(right: 10),
              child: StringUtils.isNotNull(item.isShow) && item.isShow! ? Button(
                onPressed: ()=>{item.onPressed!(index, childIndex, level)},
                label: item.label,
                height: 40,
                fontSize: 13,
                backgroundColor: item.backgroundColor??const Color.fromARGB(255, 69, 228, 194),
              ) : null,
            )
        ),
    )).toList();
  }

  static List<Widget> getChild(List<TreeItem> items, bool expand, int index, onCheck){
     if(expand && StringUtils.isNotNull(items) && items.length > 0){
       return items.mapIndexed((childIndex, item) => Row(
           children: [
             Expanded(
               flex: 3,
               child: Container(
                 height: 50,
                 decoration: const BoxDecoration(
                   border: Border(
                     bottom: BorderSide(color: Color.fromARGB(255, 243, 243, 243), width: 1.0), // 设置下边框样式
                   ),
                 ),
                 child: Padding(
                   padding: const EdgeInsets.only(left: 60),
                   child: GestureDetector(
                     onTap: ()=>{onCheck(!item.checked!, index, childIndex, 2)},
                     child: Row(
                       children: [
                         Checkbox(
                             value: item.checked??false,
                             onChanged: (value) => onCheck(value, index, childIndex, 2),
                             activeColor: const Color.fromARGB(255, 69, 228, 194)
                         ),
                         Padding(
                           padding: const EdgeInsets.only(left: 0),
                           child: Text(item.label??'', style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: Colors.grey)),
                         )
                       ],
                     ),
                   )
                 ),
               ),
             ),
             ...getButtons(item.buttons??[], index, childIndex, 2),
           ]
       )).toList();
     }
     return [];
  }
}